package com.itheima.reggie.web;

import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.IEmployeeSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    IEmployeeSevice employeeSevice;

    /**
     * 登录
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee emp,HttpSession session){
        log.info("用户登录：{}",emp);

        //1. 调用业务层验证用户登录的方法
        //具体的登录的业务逻辑在业务层方法中（建议）
        Map loginResultMap = employeeSevice.checkLogin(emp);
        Object loginEmp = loginResultMap.get("emp");

        //2. 返回登录结果
        if(loginEmp==null){
            return R.error((String)loginResultMap.get("msg"));
        }

        //3.用户登录成功后，将用户信息存储到session中
        session.setAttribute("emp",loginEmp);

        return R.success(loginEmp);
    }


    /**
     * 用户退出
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){

        //用户成功之后，将用户信息存储到session中
        //用户退出之后，将存储到session中的用户信息删除
        session.removeAttribute("emp");
        return R.success("null");
    }



}
